//
//  AppDelegate.swift
//  MyMicroblog
//
//  Created by Mac on 17/1/4.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        print(Bundle.main)
        // 注册通知
        registerNotification()
        
        // 实例化 window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        
        // 设置根控制器, 判断登录状态
        window?.rootViewController = MMGUserAccountViewModel.shared.userLogin ? MMGWelcomeController() : MMGTabBarController()

        // 设置 window 为关键 window 并显示
        window?.makeKeyAndVisible()
        
        return true
    }

    
    // 注册通知
    private func registerNotification() {
    
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: NSNotification.Name(kChoseRootViewController), object: nil)
    }
    
    // 切换根控制器
    @objc private func switchRootViewController(noti:Notification) {
        
        // 根据通知内容,判断要切换到控制器类型
        let viewCtr = noti.object == nil ? MMGTabBarController() : MMGWelcomeController()
        // 设置 tabBar
        window?.rootViewController = viewCtr
    }

    
    // 移除通知,这里的移除通知没有意义,但是作为一种规范,我们最好要写上
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

